package com.small.utils;

import java.io.*;

/**
 * 流操作工具类
 * Created by fangzheng on 2018/1/30.
 */
public final class StreamUtils {

    /**
     * 从输入流中获取字符串：将输入流转换为字符串
     *
     * @param is
     * @return
     */
    public static String getString(InputStream is) {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line = null;
        try {
            while (null != (line = reader.readLine())) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 关闭流
     * @param streams
     */
    public static void closeStream(Closeable... streams) {
        for (Closeable stream : streams) {
            try {
                if (null != stream)
                    stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
